#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;

#### input var ###

my $inputfile=undef;
my $help=undef;
my $search_client='';
my ($flag_failed,$flag_success,$flag_showfiles,$flag_showoutput)=undef;

#### var for parse ###
my $server='';
my $output='';
my $filename='';
my %hash=();
my $bos="no";
my $eos="no";
my $status="";

# Subs
GetOptions(	"inputfile=s"=>\$inputfile,
		"help|?"=>\$help,
		"showfiles"=>\$flag_showfiles,
		"showoutput"=>\$flag_showoutput,
		"failed"=>\$flag_failed,
		"success"=>\$flag_success,
		"client=s"=>\$search_client);

sub fill_hash(){
 $hash{$server}{'status'}=$status;
 $output=undef;
 $server=undef;
 $filename=undef;
 $bos="no";
 $status="";
}

# MAIN 
pod2usage(verbose=>2) if ($help);
pod2usage(verbose=>0,message=>"no inputfile") unless ($inputfile);
pod2usage(message=>"file does not exist") unless (-e $inputfile);

#### file input ###


open(CSVDATEI,$inputfile);
my @csvfile=<CSVDATEI>;
close(CSVDATEI);

#### parse ###

foreach (@csvfile){
 chomp $_;

 if ($_ =~ m/^Server,Exit code,Status,Output$|^$/) {
  next;
 } elsif ($_ =~ m/^.*,\d*,(Success|Failed),.*$/) {
  if ($eos eq "no" && $server) { $status="Failed";}
  if ($status eq "Failed") {
   $hash{$server}{'ERROR'}=$output;
   &fill_hash;
  }
  $eos="no";
  ($server,undef,$status,$output)=split(',',$_);
  $server=lc($server);
 } elsif ($_ =~ m/^#~#BOS#~#$/) {
  $bos="yes";
 } elsif ( $_ =~ m/^#~#.*#~#$/  && not $_ =~ m/^#~#(BOS|EOS)#~#$/) {
  if ($bos eq "no") { $status="Failed"; next;}
  if ($filename){
   $hash{$server}{$filename}=$output;
   $output="";
  }
  $filename=substr($_,3,-3);
 } elsif ($_ =~ m/^#~#EOS#~#$/) {
  $eos="yes";
  if ($bos eq "yes") {
   if ($filename) {
    $hash{$server}{$filename}=$output;
   }
  } else {
   $hash{$server}{'ERROR'}="no BOS found";
  }
  &fill_hash;
 } else {
  if ($bos eq "no" && $status eq "Success") { $status="Failed"; next;}
  $output.=$_ . "\n";
 }
}
if ($server){
 $hash{$server}{'error'}=$output;
 &fill_hash;
}

#### Ausagbe ###

my $show_status='';
$show_status="Failed" if ($flag_failed);
$show_status="Success" if ($flag_success);
print '<html><body><table style="border:1px solid black; empty-cells:show">';
foreach my $clientname (sort (grep { /$search_client/ } keys %hash)) {
 if (grep { $hash{$clientname}{'status'} =~ m/$show_status/} keys %hash) {
#  print $clientname."\t".$hash{$clientname}{'status'}."\n";
  print "<tr><td><b>".$clientname."</b></td><td>".$hash{$clientname}{'status'}."</td></tr>";
  if ($flag_showfiles){
   foreach my $file (sort keys %{$hash{$clientname}}) {
    print "<tr><td>Filename:</td><td><b>" . $file . "</b></td></tr>" unless ($file eq "status");
    print "<tr><td>Output:</td><td>" . $hash{$clientname}{$file} . "</td></tr>" if ($flag_showoutput && $file ne "status");
   }
  }
  print "<tr><td colspan=2><hr></td></tr>";
 }
# print "\n" . "#" x 50 . "\n";
}
print "</table></body></html>"

__END__

=pod

=head1 VERSION

Version 1.0

=head1 NAME

B<parse.pl> parser for csv

=head1 DESCRIPTION

=over 4

=item program is for cloning or staging of SSID/CloneID depending on parameter provided it could be run on one or more volumes

=back

=head1 SYNOPSIS

parse.pl -file <filename>

=head1 OPTIONS

=over 4

=item B<-file> I<filename>

=over 8

=item B<mandatory> - file to use

=back

=back

=cut